<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Content_process extends Admin_Controller{

	function Content_process(){
        parent::Admin_Controller();
        $CI = & get_instance();    
	}
	
	function index(){
		//$this->change_label();
	}
    
	function change_label()
	{
		global $CI;
		$CI->load->module_library('users.auth');
        $auth_data = $CI->auth->get_auth_data();
		$branch_id = $CI->input->post('branch_id');
		$menu_id = split("_",$branch_id);		
		if(is_numeric($menu_id[0])){	
			$navi_name = $CI->input->post('new_value');			
			$update = "UPDATE ".NAVIGATION." SET ".NAVIGATION_NAME." = '".$navi_name."'";
			$cond = ' WHERE '.NAVIGATION_FRANCHISE_ID.' = '.$auth_data['admin_id'];
			$cond .= ' AND '.NAVIGATION_MENU_ID.' = '.$menu_id[0];
			
			$sql = $update.$cond;
			$CI->db->query($sql);			
			echo $navi_name;		
			$this->view('_blank');
		}
		else
			$this->change_title();
	}
	function change_title()
	{
		global $CI;
		$CI->load->module_library('users.auth');
        $auth_data = $CI->auth->get_auth_data();
		if ($CI->input->post('branch_id')) {
			$page_id = split("_",$CI->input->post('branch_id'));
			$title = $CI->input->post('new_value');	
			$new_title = trim(str_replace("page","",$title));		
			$update = "UPDATE ".PAGE." SET ".PAGE_TITLE." = '".$new_title."'";
			$cond = ' WHERE '.PAGE_FRANCHISE_ID.' = '.$auth_data['admin_id'];
			$cond .= ' AND '.PAGE_PAGE_ID.' = '.$page_id[1];
			
			$sql = $update.$cond;
			$CI->db->query($sql);			
			echo $title;
		}
		$this->view('_blank');
	}
	
	function change_parrent()
	{
		global $CI;
		$this->view('_blank');
		$CI->load->module_library('users.auth');
        $auth_data = $CI->auth->get_auth_data();

		if ($CI->input->post('drag_id')) {
			$drag_id = split("_",$CI->input->post('drag_id'));
			$drop_id = split("_",$CI->input->post('drop_id'));
			
            $cond = array( NAVIGATION_FRANCHISE_ID => $auth_data['admin_id'],
                           NAVIGATION_MENU_ID =>$drag_id[0]
                        );
            $res = $CI->db->get_where(NAVIGATION, $cond);
            if($res->num_rows() > 0)
                $drag_nav = $res->row();
            
            $cond2 = array( NAVIGATION_FRANCHISE_ID => $auth_data['admin_id'],
                           NAVIGATION_MENU_ID =>$drop_id[0]
                        );
            $res2 = $CI->db->get_where(NAVIGATION, $cond2);
            if($res2->num_rows() > 0)
                $drop_nav = $res2->row();
            // Can not drag MAIN menu item to a child menu item
            if(empty($drag_nav->{NAVIGATION_PAR_MENU_ID}) && !empty($drop_nav->{NAVIGATION_PAR_MENU_ID})){
                echo '0';
                return false;
            }
            // If drag a MAIN menu item to another MAIN menu item --> switch order
            if(empty($drag_nav->{NAVIGATION_PAR_MENU_ID}) && empty($drop_nav->{NAVIGATION_PAR_MENU_ID})){
                return $this->switch_order($drag_nav, $drop_nav);
            }
            
            $set = array(NAVIGATION_PAR_MENU_ID => $drop_id[0]);
			$CI->db->set($set);
            $CI->db->where($cond);
            $CI->db->update(NAVIGATION);
            echo '<success>1</success>';
            return true;
		}
	}
	
    function switch_order($drag_nav, $drop_nav){
        global $CI;
        
        $data = array(NAVIGATION_SUB_MENU_ID => $drop_nav->{NAVIGATION_SUB_MENU_ID});
        
        /*if($drop_nav->{NAVIGATION_SUB_MENU_ID} > $drag_nav->{NAVIGATION_SUB_MENU_ID} )
            $direction = -1;
        else
            $direction = 1;
        // Update all menu item's order -> order+1
        $sql = 'UPDATE '.NAVIGATION.' SET '.NAVIGATION_SUB_MENU_ID.' = '.NAVIGATION_SUB_MENU_ID.($direction>0?' + 1 ':' - 1 ').' 
                WHERE '.NAVIGATION_FRANCHISE_ID.'='.$drop_nav->{NAVIGATION_FRANCHISE_ID}.' 
                AND '.NAVIGATION_PAR_MENU_ID.' = 0 
                AND '.NAVIGATION_SUB_MENU_ID.($direction>0?' >= ':' <= ').$drop_nav->{NAVIGATION_SUB_MENU_ID};
        $CI->db->query($sql);*/
        
        if($drop_nav->{NAVIGATION_SUB_MENU_ID} > $drag_nav->{NAVIGATION_SUB_MENU_ID} ){
            // Move down
            $sql = 'UPDATE '.NAVIGATION.' SET '.NAVIGATION_SUB_MENU_ID.' = '.NAVIGATION_SUB_MENU_ID.' - 1 
                WHERE '.NAVIGATION_FRANCHISE_ID.'='.$drop_nav->{NAVIGATION_FRANCHISE_ID}.' 
                AND '.NAVIGATION_PAR_MENU_ID.' = 0 
                AND '.NAVIGATION_SUB_MENU_ID.'>='.$drag_nav->{NAVIGATION_SUB_MENU_ID}.' AND '.NAVIGATION_SUB_MENU_ID.' <= '.$drop_nav->{NAVIGATION_SUB_MENU_ID};
        }else{
            // Move up
            $sql = 'UPDATE '.NAVIGATION.' SET '.NAVIGATION_SUB_MENU_ID.' = '.NAVIGATION_SUB_MENU_ID.' + 1 
                WHERE '.NAVIGATION_FRANCHISE_ID.'='.$drop_nav->{NAVIGATION_FRANCHISE_ID}.' 
                AND '.NAVIGATION_PAR_MENU_ID.' = 0 
                AND '.NAVIGATION_SUB_MENU_ID.' <= '.$drag_nav->{NAVIGATION_SUB_MENU_ID}.' AND '.NAVIGATION_SUB_MENU_ID.' >= '.$drop_nav->{NAVIGATION_SUB_MENU_ID};
        }

        $CI->db->query($sql);
        
        $cond = array( NAVIGATION_FRANCHISE_ID => $drag_nav->{NAVIGATION_FRANCHISE_ID},
                       NAVIGATION_MENU_ID => $drag_nav->{NAVIGATION_MENU_ID}
                    );
        $CI->db->set($data);
        $CI->db->where($cond);
        $CI->db->update(NAVIGATION);
        echo '<success>1</success>';
        return true;
    }
	function add_submenu()
	{
		global $CI;
		$CI->load->module_library('users.auth');
        $auth_data = $CI->auth->get_auth_data();
		
		if ($CI->input->post('par_id')) {
			$par_menu_id = $CI->input->post('par_id');
			$CI->db->select_max(NAVIGATION_MENU_ID);
			$q = $CI->db->get_where(NAVIGATION, array(NAVIGATION_FRANCHISE_ID=>$auth_data['admin_id']));
			$row = $q->row();
			$max_id = $row->{NAVIGATION_MENU_ID}+1;
			
            $order_number = 1;
            $CI->db->select_max(NAVIGATION_SUB_MENU_ID);
            $q = $CI->db->get_where(NAVIGATION, array(NAVIGATION_FRANCHISE_ID=>$auth_data['admin_id'], NAVIGATION_PAR_MENU_ID=>$par_menu_id));
            if($q->num_rows() > 0){
                $order_number = $q->row()->{NAVIGATION_SUB_MENU_ID} + 1;
            }
			$sql = "INSERT ".NAVIGATION." ( ".NAVIGATION_MENU_ID ." , ". NAVIGATION_FRANCHISE_ID ." , ".NAVIGATION_NAME." , ";
			$sql .= NAVIGATION_TOPIC." , ".NAVIGATION_PAR_MENU_ID. ", ".NAVIGATION_SUB_MENU_ID.")";
			$sql .= " VALUES ( ".$max_id.",".$auth_data['admin_id'].",'"."New Menu"."','"."New Menu"."',".$par_menu_id.", ".$order_number.")";
			if($CI->db->query($sql))
			{
				echo '<success>1</success>';
			}
			
		}else
            echo 'Please specify parent menu item.';
		
		$this->view('_blank');
	}   
	function add_page()
	{
		global $CI;
		$CI->load->module_library('users.auth');
		
        $auth_data = $CI->auth->get_auth_data();
		$CI->load->module_model('content_management.page2_detail_model');
		if ($CI->uri->segment(3)) {
			$menu_id = $CI->uri->segment(3);	
			$pid = $CI->page2_detail_model->maxkey();		
			$page_title = 'New';

			$select = 'SELECT '.NAVIGATION_MENU_ID.', '.NAVIGATION_NAME;
			$from = ' FROM '.NAVIGATION;
			$cond = ' WHERE '.NAVIGATION_FRANCHISE_ID.' = '.$auth_data['admin_id'];
			$cond .= ' AND '.NAVIGATION_MENU_ID.' = '.$menu_id;
			
			$sql = $select.$from.$cond;
			$results = $CI->db->query($sql)->row();
			if($results) $page_title = $results->name;
			$insert = "INSERT ".PAGE." ( ".PAGE_PAGE_ID.",  ";
			$insert .= PAGE_FRANCHISE_ID.",".PAGE_TITLE." ) ";
			$insert .= " VALUES( ".$pid." , ".$auth_data['admin_id'].",'".$page_title."' ) ";
			if($CI->db->query($insert)){
				if(is_numeric($menu_id)){
					$update = "UPDATE ".NAVIGATION." SET ".NAVIGATION_PID." = ".$pid.", ";
					$update .= NAVIGATION_URL." = '?fid=".$auth_data['admin_id']."&amp;pid=".$pid." ' ";
					$cond = " WHERE ".NAVIGATION_FRANCHISE_ID." = ".$auth_data['admin_id'];
					$cond .= " AND ".NAVIGATION_MENU_ID." = ".$menu_id;
					$sql = $update.$cond;
					$CI->db->query($sql);
				}
			}	
			echo '<success>1</success>';
		}else
            echo 'Please specify menu item.';
		$this->view('_blank');
	} 
	function del_page()
	{
		global $CI;
		$CI->load->module_library('users.auth');
        $auth_data = $CI->auth->get_auth_data();
		if ($CI->uri->segment(3)) {			
			$page_id = $CI->uri->segment(3);			
			$delete = "DELETE FROM ".PAGE."  ";
			$cond = " WHERE ".PAGE_FRANCHISE_ID." = ".$auth_data['admin_id'] . " ";
			$cond .= " AND ".PAGE_PAGE_ID." = ".$page_id;
			$sql = $delete.$cond;
			if($CI->db->query($sql))
			{			
				$pid = 0;		
				$update = "UPDATE ".NAVIGATION." SET ".NAVIGATION_PID." = 0, ";
				$update .= NAVIGATION_URL." = '' ";
				$cond = ' WHERE '.NAVIGATION_FRANCHISE_ID.' = '.$auth_data['admin_id'];
				$cond .= ' AND '.NAVIGATION_PID.' = '.$page_id;
				
				$sql = $update.$cond;
				$CI->db->query($sql);
			}
			echo '<success>1</success>';
		}else
            echo 'Page not found.';
		$this->view('_blank');
	}
    
    function del_navigation()
	{
		global $CI;
        $this->view('_blank');
		$CI->load->module_library('users.auth');
        $auth_data = $CI->auth->get_auth_data();
		if ($CI->uri->segment(3)) {
			$nav_id = $CI->uri->segment(3);
            
            // Check if there are submenu item
            $chk_conditions = array(NAVIGATION_FRANCHISE_ID=>$auth_data['admin_id'], NAVIGATION_PAR_MENU_ID=>$nav_id);
            $CI->db->where($chk_conditions);
            if($CI->db->count_all_results(NAVIGATION)){
                echo 'Please remove all submenu item(s) first.';
                return;
            }
            
            
            $conditions = array(NAVIGATION_FRANCHISE_ID=>$auth_data['admin_id'], NAVIGATION_MENU_ID=>$nav_id);
            $CI->db->where($conditions);
            $result = $CI->db->get(NAVIGATION);
            if($result->num_rows()>0){
                $navigation = $result->row();
                // Check if it is MAIN ITEM
                if(empty($navigation->{NAVIGATION_PAR_MENU_ID})){
                    echo 'Can not delete MAIN MENU item.';
                    return;
                }
                // Check if there is page related to this menu item
                if(!empty($navigation->{NAVIGATION_PID})){
                    echo 'Please remove related page first.';
                    return;
                }
                // Delete navigation
                $CI->db->where($conditions);
                $CI->db->delete(NAVIGATION);
                echo '<success>1</success>';
            }
        }else
            echo 'Menu item not found.';
    }   
    
    function move_up(){
        global $CI;
        $CI->load->module_library('users.auth');
        $auth_data = $CI->auth->get_auth_data();
        $this->view('_blank');
        
        $nav_id = $CI->input->post('nav_id');
        $chk_conditions = array(NAVIGATION_FRANCHISE_ID=>$auth_data['admin_id'], NAVIGATION_MENU_ID=>$nav_id);
        $CI->db->where($chk_conditions);
        if($CI->db->count_all_results(NAVIGATION) == 0){
            echo 'Navigation not found.';
            return;
        }
        $nav = $CI->db->get_where(NAVIGATION, $chk_conditions)->row();
        // Find the upper menu item
        $CI->db->limit(1);
        $CI->db->order_by(NAVIGATION_SUB_MENU_ID, 'DESC');
        $CI->db->where(NAVIGATION_FRANCHISE_ID, $auth_data['admin_id']);
        $CI->db->where(NAVIGATION_PAR_MENU_ID, $nav->{NAVIGATION_PAR_MENU_ID});
        $CI->db->where(NAVIGATION_SUB_MENU_ID.' < ' ,$nav->{NAVIGATION_SUB_MENU_ID});
        $result = $CI->db->get(NAVIGATION);
        if($result->num_rows() > 0){
            $upper_nav = $result->row();
            // Update order of this menu item
            $chk_conditions = array(NAVIGATION_FRANCHISE_ID=>$auth_data['admin_id'], NAVIGATION_MENU_ID=>$nav_id);
            $CI->db->set(array(NAVIGATION_SUB_MENU_ID => $upper_nav->{NAVIGATION_SUB_MENU_ID}));
            $CI->db->where($chk_conditions);
            $CI->db->update(NAVIGATION);
            // Update order of upper menu item
            $chk_conditions = array(NAVIGATION_FRANCHISE_ID=>$auth_data['admin_id'], NAVIGATION_MENU_ID=>$upper_nav->{NAVIGATION_MENU_ID});
            $CI->db->set(array(NAVIGATION_SUB_MENU_ID => $nav->{NAVIGATION_SUB_MENU_ID}));
            $CI->db->where($chk_conditions);
            $CI->db->update(NAVIGATION);
            echo '<success>1</success>';
            return;
        }
        echo '<nochange>1</nochange>';
    }
    
    function move_down(){
        global $CI;
        $CI->load->module_library('users.auth');
        $auth_data = $CI->auth->get_auth_data();
        $this->view('_blank');
        
        $nav_id = $CI->input->post('nav_id');
        $chk_conditions = array(NAVIGATION_FRANCHISE_ID=>$auth_data['admin_id'], NAVIGATION_MENU_ID=>$nav_id);
        $CI->db->where($chk_conditions);
        if($CI->db->count_all_results(NAVIGATION) == 0){
            echo 'Navigation not found.';
            return;
        }
        $nav = $CI->db->get_where(NAVIGATION, $chk_conditions)->row();

        // Find the below menu item
        $CI->db->limit(1);
        $CI->db->order_by(NAVIGATION_SUB_MENU_ID, 'ASC');
        $CI->db->where(NAVIGATION_FRANCHISE_ID, $auth_data['admin_id']);
        $CI->db->where(NAVIGATION_PAR_MENU_ID, $nav->{NAVIGATION_PAR_MENU_ID});
        $CI->db->where(NAVIGATION_SUB_MENU_ID.' > ' ,$nav->{NAVIGATION_SUB_MENU_ID});
        $result = $CI->db->get(NAVIGATION);

        if($result->num_rows() > 0){
            $below_nav = $result->row();

            // Update order of this menu item
            $chk_conditions = array(NAVIGATION_FRANCHISE_ID=>$auth_data['admin_id'], NAVIGATION_MENU_ID=>$nav_id);
            $CI->db->set(array(NAVIGATION_SUB_MENU_ID => $below_nav->{NAVIGATION_SUB_MENU_ID}));
            $CI->db->where($chk_conditions);
            $CI->db->update(NAVIGATION);
            // Update order of lower menu item
            $chk_conditions = array(NAVIGATION_FRANCHISE_ID=>$auth_data['admin_id'], NAVIGATION_MENU_ID=>$below_nav->{NAVIGATION_MENU_ID});
            $CI->db->set(array(NAVIGATION_SUB_MENU_ID => $nav->{NAVIGATION_SUB_MENU_ID}));
            $CI->db->where($chk_conditions);
            $CI->db->update(NAVIGATION);
            echo '<success>1</success>';
            return;
        }
        echo '<nochange>1</nochange>';
    }
}